package com.gzyj.server.factory.jtt808.up;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.common.GPSPosition;
import com.gzyj.jtt808.v2011.additionalinfo.JTT808_AddtionalInfo;
import com.gzyj.jtt808.v2011.protocol.subiao.JTT808_0x0200_0x64;
import com.gzyj.jtt808.v2011.protocol.subiao.JTT808_0x0200_0x65;
import com.gzyj.jtt808.v2013.HeaderMsgBodyProperty;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2013.enums.JT808TerminalResult;
import com.gzyj.jtt808.v2013.protocol.JTT808_0x0200;
import com.gzyj.jtt808.v2013.protocol.JTT808_0x8001;
import com.gzyj.server.config.LogConfig;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.factory.jtt808.NetWorkSerrvice;
import com.gzyj.server.service.CommonService;
import com.gzyj.server.service.GPSService;
import com.gzyj.server.service.RoadLevelAlarmService;
import com.gzyj.server.service.ZdaqService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author lhj
 */
@Component
public class JTT808_0x0200_Handler implements CommonHandler {

    @Autowired
    private NetWorkSerrvice netWorkSerrvice;



    @Autowired
    private CommonService commonService;

    @Autowired
    private GPSService gpsService;

    @Autowired
    private RoadLevelAlarmService roadLevelAlarmService;


    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {
        long start = System.currentTimeMillis();
        JTT808_0x0200 jtt808_0x0200 = (JTT808_0x0200) msg;
        JTT808_0x8001 jtt808_0x8001 = new JTT808_0x8001();

        HeaderMsgBodyProperty headerMsgBodyProperty = new HeaderMsgBodyProperty();
        headerMsgBodyProperty.setSubcontract(jtt808_0x0200.getPackageHeader().getMsgBodyProperty().isSubcontract());
        headerMsgBodyProperty.setEncryptMethod(jtt808_0x0200.getPackageHeader().getMsgBodyProperty().getEncryptMethod());
        jtt808_0x8001.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
        jtt808_0x8001.getPackageHeader().setTerminalPhoneNo(jtt808_0x0200.getPackageHeader().getTerminalPhoneNo());

        jtt808_0x8001.setMsgNum(jtt808_0x0200.getPackageHeader().getMsgNum());
        jtt808_0x8001.setAckMsgId(jtt808_0x0200.getPackageHeader().getMsgId());
        jtt808_0x8001.setResult(JT808TerminalResult.成功_确认);
        netWorkSerrvice.send2Veh(jtt808_0x0200.getPackageHeader().getTerminalPhoneNo(), jtt808_0x8001.encode());

        GPSPosition gpsPosition = jtt808_0x0200.transform();

        VehicleIncrement vehicleInfo = commonService.getVehInfo(jtt808_0x0200.getPackageHeader().getTerminalPhoneNo());
        if(vehicleInfo!=null){
            gpsService.processZdaq(vehicleInfo, gpsPosition);
            roadLevelAlarmService.processRoadLevelLimited(jtt808_0x0200,vehicleInfo,null);
        }


        LogConfig.commondLog.info("808 2013版 位置汇报信息 执行耗时：{} ms", System.currentTimeMillis() - start);
    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {
        long start = System.currentTimeMillis();
        com.gzyj.jtt808.v2019.protocol.JTT808_0x0200 jtt808_0x0200 = (com.gzyj.jtt808.v2019.protocol.JTT808_0x0200) msg;

        com.gzyj.jtt808.v2019.protocol.JTT808_0x8001 jtt808_0x8001 = new com.gzyj.jtt808.v2019.protocol.JTT808_0x8001();
        com.gzyj.jtt808.v2019.HeaderMsgBodyProperty headerMsgBodyProperty = new com.gzyj.jtt808.v2019.HeaderMsgBodyProperty();

        headerMsgBodyProperty.setSubcontract(jtt808_0x0200.getPackageHeader().getMsgBodyProperty().isSubcontract());
        headerMsgBodyProperty.setEncryptMethod(jtt808_0x0200.getPackageHeader().getMsgBodyProperty().getEncryptMethod());
        headerMsgBodyProperty.setVersionFlag(jtt808_0x0200.getPackageHeader().getMsgBodyProperty().isVersionFlag());
        jtt808_0x8001.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
        jtt808_0x8001.getPackageHeader().setTerminalPhoneNo(jtt808_0x0200.getPackageHeader().getTerminalPhoneNo());
        jtt808_0x8001.getPackageHeader().setProtocolVersion(jtt808_0x0200.getPackageHeader().getProtocolVersion());

        jtt808_0x8001.setMsgNum(jtt808_0x0200.getPackageHeader().getMsgNum());
        jtt808_0x8001.setAckMsgId(jtt808_0x0200.getPackageHeader().getMsgId());
        jtt808_0x8001.setResult(JT808TerminalResult.成功_确认);

        netWorkSerrvice.send2Veh(jtt808_0x0200.getPackageHeader().getTerminalPhoneNo(), jtt808_0x8001.encode());

        GPSPosition gpsPosition = jtt808_0x0200.transform();
        VehicleIncrement vehicleInfo = commonService.getVehInfo(jtt808_0x0200.getPackageHeader().getTerminalPhoneNo());
        if(vehicleInfo!=null){
            gpsService.processZdaq(vehicleInfo, gpsPosition);
            roadLevelAlarmService.processRoadLevelLimited(jtt808_0x0200,vehicleInfo,null);
        }


        LogConfig.commondLog.info("808 2019版 位置汇报信息 执行耗时：{} ms", System.currentTimeMillis() - start);

    }






    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }

    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {

    }
}
